import cfg from "../config";
import {sid, tab, mobile, get, post, getUser, docker} from '../util/api';
import {tabSwitch, getRosterList, jumpUrl} from "../util/aid";
import * as ajax from "../lib/kajax";
const $ = require('../lib/kdom');
let _view = require('raw-loader!./sign.html'); // eslint-disable-line

let loginStop = true;

let sigininStop = true;

export default class {
  path = '/ogspc/sign$';
  hash = '/ogspc/sign';
  title = '工银重庆对公客户信息平台';

  constructor(opt) {
    opt = opt || {};
    this.path = opt.path || this.path;
    this.hash = opt.hash || this.hash;
    this.title = opt.title || this.title;
  }

  // 输出视图
  view(cb) {
    if (!_view) {
      // 静态资源浏览器有缓存,增加时标,强制刷新!
      getView(`${cfg.view.signin}?t=${(+new Date())}`, '', (rs) => {
        _view = rs;
        cb(null, _view);
      });
    } else
      cb(null, _view);
  }

  // 在已经加载的视图上操作
  bind(dv, params) {
    let loginMobile = $.id('loginMobile');
    let loginPassword = $.id('loginPassword');

    let sigininMobile = $.id('sigininMobile');
    let sigininPassword = $.id('sigininPassword');
    let sigininRePassword = $.id('sigininRePassword');

    $.id('loginBtn').onclick = () => {
      if (!loginMobile.value) {
        return alert('手机号不能为空!');
      }
      if (!loginPassword.value) {
        return alert('密码不能为空!');
      }
      mobile(loginMobile.value);
      getUser(`${HOST_URL}${cfg.ogs.userInfo}`, `mobile=${mobile()}`, (rs) => {
        if (rs) {
          console.log('getUser [rs] ', rs);
          mobile(rs.mobile);
          $.router.go('./ogspc/home', {user: rs}, true);
        } else {
          alert('获取身份失败,请退出重新进入或联系客服!');
        }
      });

      // login(loginMobile.value, loginPassword.value, params);
    };

    $.id('sigininBtn').onclick = () => {
      if (!sigininMobile.value) {
        return alert('手机号不能为空!');
      }
      if (!sigininPassword.value) {
        return alert('密码不能为空!');
      }
      if (sigininPassword.value != sigininRePassword.value) {
        return alert('密码输入不一致!');
      }
      siginin(sigininMobile.value, sigininPassword.value, params);
    };

    // // tab 切换
    // tabSwitch($.qus('#loginTabBox .login-tab-link'), (that) => {
    //     let val = that.getAttribute('data-val');
    //     console.log(val);
    //     if (val == 1) {
    //         $.id('signUpBox').style.display = 'block';
    //         $.id('signInBox').style.display = 'none';
    //     } else if (val == 2) {
    //         $.id('signUpBox').style.display = 'none';
    //         $.id('signInBox').style.display = 'block';
    //     }
    // });
  }

  /*bind*/
}

function login(mobile, password, params) {
  if (loginStop) {
    loginStop = false;
    ajax.post(`${HOST_URL}${cfg.ogs.login}`, `sid=${sid()}&tab=${tab()}&mobile=${mobile}&password=${password}`, function (err, rs) {
      console.log(rs);
      if (rs) {
        sessionStorage.setItem(cfg.app.nuoyaMobile, mobile);
        jumpUrl(mobile, (user, roster) => {
          $.router.go('./lottery/home', {user: user, roster: roster}, true);
        });
      } else {
        loginStop = true;
      }
    });
  } else {
    alert('正在登录，请稍后！');
  }
}

function siginin(mobile, password, params) {
  if (sigininStop) {
    sigininStop = false;
    ajax.post(`${HOST_URL}${cfg.ogs.siginin}`, `sid=${sid()}&tab=${tab()}&mobile=${mobile}&password=${password}`, function (err, rs) {
      console.log(rs);
      if (rs) {
        sessionStorage.setItem(cfg.app.nuoyaMobile, mobile);
        jumpUrl(mobile, (user, roster) => {
          $.router.go('/ogspc/home', {user: user, roster: roster}, true);
        });
      } else {
        sigininStop = true;
      }
    });
  } else {
    alert('正在注册，请稍后！');
  }
}


// function getRoster() {
//     get(HOST_URL + '/user/api/getRoster', 'mobile=13340233396&sid=11&tab=1', rs => {
//         console.log(rs);
//     });
// }
// function getRosterList() {
//
//     get(HOST_URL + '/user/api/getRosterList', 'sid=11&tab=1', rs => {
//         console.log(rs);
//     });
// }